<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>post请求</title>
</head>
<body>
<script type="text/javascript">
    window.onload=function (){
        document.getElementById("mybtn").onclick=function (){
           //1、创建AJAX 核心对象
            var xhr = new XMLHttpRequest();
            //2、注册回调函数
            xhr.onreadystatechange=function (){

                if (this.readyState==4){
                    if (this.status==200){
                        document.getElementById("mydiv").innerHTML=this.responseText
                    }
                }
            }
            //3、开启通道
            xhr.open("POST","/ajax/ajaxrequest3",true)

            //4、发送请求

            //模拟Ajax 提交from表单? 设置请求头内容类型(关键代码)
            xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded")

            //使用js 代码获取数据
            var name = document.getElementById("username").value;
            var password = document.getElementById("password").value;

            xhr.send("username="+name+"&password="+password);
        }
    }

</script>用户名<input type="text" id="username"><br>
密码<input type="password" id="password"><br>
<button id="mybtn">发送 post请求</button><br>
<div id="mydiv"></div>

</body>
</html>